package net.comic.entities

class Page {
	//	Persistence Attributes
	String 	name
	String 	description
	String 	imagePath
	Integer numberPage
	boolean rateable = true
	boolean disabled = false

	Date 	dateCreated
	Date 	lastUpdated
	

	//	Not Persistence Attributes
	static 	transients = ['rate']

	
	//	DB Relations
	static belongsTo = 	[comic:			Comic,
	                   	 createdBy:		User]
	
	static hasMany =	[alterPages:	AlterPage,
	                	 ratings:		Rate,
	                	 comments:		Comment]
	

	//	DB Constraits
	static 	constraints = {
		name(blank:false, unique:true, maxSize:50)
		description(nullable:true, maxSize:1000)
	}
	

	//	Other Methods
	String getRate() {
		//	Calculem la mitja de les puntuacions rebudes fins ara
		Float p = 0
		if(ratings){
			ratings.each(){ p += it.rate }
			p = p / ratings.count()
		}
		return p	//TODO: comprvar si esta linea hace falta
	}
}